#include <iostream>
using namespace std;
#include "SmartPtr.h"

//智能指针：
//RAII:利用对象的生命周期来控制资源
//像指针一样使用
//拷贝问题

template<class T>
class SmartPtr{
public:
    SmartPtr(T* ptr):_ptr(ptr){}

    ~SmartPtr(){
        cout << "delete:" << _ptr << endl;
        delete _ptr;
    }

    T& operator*(){
        return *_ptr;
    }

    T* operator->(){
        return _ptr;
    }

private:
    T* _ptr;
};

int Div(int a, int b){
    if (b == 0)
        throw invalid_argument("Divide by zero");
    return a / b;
}

void func(){
    SmartPtr<int> sp1(new int(10));
    SmartPtr<int> sp2(new int(20));

    cout << Div(4, 0) << endl;

    *sp1 = 100;

    cout << *sp1 << endl;
    cout << *sp2 << endl;
}

void test(){
    try {
        func();
    }
    catch (exception& e){
        cout << e.what() << endl;
    }
}

void test1(){
    SmartPtr<int> sp1(new int(10));
    SmartPtr<int> sp2(sp1);
}

int main() {
    //test1();
    //Lu::testAuto();
    //Lu::testUnique();
    //Lu::testShared();
    //Lu::testSharedSafe();
    Lu::testSharedDeleter();
    return 0;
}
